package com.twu.biblioteca.service;

import com.twu.biblioteca.exceptions.AssetUnavailableException;
import com.twu.biblioteca.exceptions.MovieDoesNotBelongToLibraryException;
import com.twu.biblioteca.model.Library;
import com.twu.biblioteca.model.LibraryService;
import com.twu.biblioteca.model.ServiceResult;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by davecroman on 19/09/2014.
 */
public class CheckoutMovieService extends LibraryService {

    public CheckoutMovieService(Library library){
        super(library);
    }

    @Override
    public ServiceResult performService() {
        System.out.print("Enter movie id to checkout: ");
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));

        try {
            String input = bufferRead.readLine();
            Integer movieId = Integer.parseInt(input);
            return checkOutMovie(movieId);
        } catch (IOException e) {
            return new ServiceResult("Checkout Failed: Input Invalid");
        }
    }

    public ServiceResult checkOutMovie(Integer movieId){
        try {
            library.checkOutMovie(movieId, user);
        } catch (Exception e) {
            return new ServiceResult(e.getMessage());
        }

        return new ServiceResult("You have successfully checked out the movie.");
    }

    @Override
    public String getName() {
        return "Checkout Movie";
    }
}
